from flask import *

app = Flask(__name__)

user_student = [
    {"sid": 1, "name": "XianMo", "age": 18, "gender": "男"},
    {"sid": 2, "name": "Jie", "age": 28, "gender": "女"},
    {"sid": 3, "name": "Fu", "age": 38, "gender": "女"}
]


@app.route("/")
def index():
    che_student = []
    che_name = request.args.get("che")  # 获取查询的名字
    if che_name:  # 如果有查询，则判断是否存在
        for stu in user_student:
            if che_name in stu.get("name"):
                che_student.append(stu)
        if not che_student:
            return render_template("index.html", user=user_student, error_message="查找不存在")
        else:
            return render_template("index.html", user=che_student)

    # 获取要删除人的sid
    del_sid = request.args.get("del")
    if del_sid:
        for stu in user_student:
            if int(del_sid) == stu.get("sid"):
                user_student.remove(stu)

    # redirect("/") 返回url
    # redirect(url_for("index")) 返回html
    # 这个不能用重定向，使用重定向会使页面无限重定向
    # 必须使用渲染,只有渲染才能传输数据
    return render_template("index.html", user=user_student)


@app.route("/add", methods=["GET", "POST"])
def stu_add():
    if request.method == "POST":
        sid = request.form["sid"]
        name = request.form["name"]
        age = request.form["age"]
        gender = request.form["gender"]

        if sid.isdigit() and age.isdigit():  # sid、age需要int类型
            sid = int(sid)
            age = int(age)
            if sid > 0 and 0 < age < 200:
                for item in user_student:  # 判断sid是重复
                    if sid == item["sid"]:
                        return render_template("add.html", error_message="错误信息: 学号重复")
                if name.isdigit():
                    return render_template("add.html", error_message="错误信息: 姓名不能只是数字")
                if gender == "男" or gender == "女":  # gander必须是男/女
                    new_student = {"sid": sid, "name": name, "age": age, "gender": gender}
                    user_student.append(new_student)

                    return redirect(url_for("index"))
                else:
                    return render_template("add.html", error_message="错误信息: 性别只能填男/女")
            return render_template("add.html", error_message="错误信息: 学号和年龄的取值范围有问题")
        else:
            return render_template("add.html", error_message="错误信息: 学号和年龄只能是数字")

    return render_template("add.html")


@app.route("/cha/<int:sid>", methods=["GET", "POST"])
def stu_cha(sid):
    if request.method == "GET":  # GET请求，打印原本的信息
        for stu in user_student:
            if sid == stu.get("sid"):
                return render_template("cha.html", user=stu)
    else:   # 获取新的数据
        new_sid = int(request.form["sid"])
        name = request.form["name"]
        age = request.form["age"]
        gender = request.form["gender"]

        new_stu = []    # 获取原本的数据，以至于错误时，返回原本的数据
        for stu in user_student:
            if sid == stu.get("sid"):
                new_stu = stu

        if age.isdigit():
            age = int(age)

            if age < 0 or age > 200:
                return render_template("cha.html", error_message="错误信息: 年龄的取值范围有问题", user=new_stu)

            if name.isdigit():
                return render_template("cha.html", error_message="错误信息: 姓名不能只是数字", user=new_stu)

            if gender == "男" or gender == "女":
                new_student = {"sid": new_sid, "name": name, "age": age, "gender": gender}

                for i in range(0, len(user_student)):   # 把新数据写入到user_student中
                    if sid == user_student[i]["sid"]:
                        user_student[i] = new_student

                return redirect(url_for("index"))
            else:
                return render_template("cha.html", error_message="错误信息: 性别只能填男/女", user=new_stu)
        else:
            return render_template("cha.html", error_message="错误信息: 年龄只能是数字", user=new_stu)


if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)
